package xj.graph2d.app.resources;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import xj.graph2d.shapes.RectangularShape;
import xj.graph2d.shapes.ShapeConstants;

import org.jvnet.flamingo.common.icon.ResizableIcon;

public class RectangularShapeIcon 
  implements ResizableIcon {

  public RectangularShapeIcon() {
    this.width = 16;
    this.height = 16;
  }

  public RectangularShapeIcon(int shape) {
    this(shape, 16, 16);
  }

  public RectangularShapeIcon(int shape, int width, int height) {
    this.width = width;
    this.height = height;
    setShape(shape);
    // rshape = new RectangularShape(shape, RectangularShape.NORTH);

  }

  public void setDimension(Dimension newDimension) {
    if (newDimension != null) { 
      dim = newDimension;
      width = dim.width;
      height = dim.height;
    }
  }

  public void setShape(int shape) {
    rshape = new RectangularShape(shape);
    dim = new Dimension(width, height);
    int s = rshape.getShapeType();
    int d1 = 0;
    int d2 = 0;
    if (s == ShapeConstants.ACTOR_SHAPE) {
      d1 = 2;
      d2 = 0;
    } else if (s == ShapeConstants.DIAMOND_SHAPE || 
	       s == ShapeConstants.OCTAGON_SHAPE) {
      d1 = 2;
      d2 = 2;
    } else {
      d1 = 1;
      d2 = 2;
    }
    rshape.setEnds(d1, d2, width - 2 * d1, height - 2 * d2);
  }

  public RectangularShape getShape() {
    return rshape; 
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    if (rshape != null) {
      Graphics2D g2 = (Graphics2D) g;
      Color oldColor = g2.getColor();
      g2.setColor(Color.black);
      rshape.move(x, y);
      rshape.setShadow(false, true);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			  RenderingHints.VALUE_ANTIALIAS_ON);
      rshape.drawShape(g2);
      rshape.move(- x, - y);
      g2.setColor(oldColor);
    }
  }

  public int getIconWidth() {
    return dim.width;
  }

  public int getIconHeight() {
    return dim.height;
  }

  protected int width, height;

  protected Dimension dim;

  protected RectangularShape rshape;

}
